//
//  NetworkMonitoring.swift
//  SwiftNetwork
//
//  Created by 万孟超 on 2022/1/13.
//

import UIKit
import Alamofire
import CoreTelephony

class NetworkMonitoring: NSObject {
    
    static let `default` = NetworkMonitoring()
    
    let monitoringManager : NetworkReachabilityManager? = NetworkReachabilityManager.default
        
    private(set) var currentNetworkState : NetworkStatus = .NetworkStatusUnkown
    
    public enum NetworkStatus {
        case NetworkStatusUnkown
        case NetworkStatusNoNet
        case NetworkStatusWIFI
        case NetworkStatusCellular(CellularNetworkState)
    }
    public enum CellularNetworkState {
        case CellularNetworkStateUnkown
        case CellularNetworkState2G
        case CellularNetworkState3G
        case CellularNetworkState4G
        case CellularNetworkState5G
    }
    func startMonitoring() {
        let queue = DispatchQueue(label: "com.CodingMan.NetworkQueue")
        monitoringManager?.startListening(onQueue: queue, onUpdatePerforming: { networkStatus in
            switch networkStatus {
            case .unknown:
                print("未知网络")
                self.currentNetworkState = .NetworkStatusUnkown
            case .notReachable:
                print("无网络")
                self.currentNetworkState = .NetworkStatusNoNet
            case .reachable(.ethernetOrWiFi):
                print("WIFI网络")
                self.currentNetworkState = .NetworkStatusWIFI
                NotificationCenter.default.post(name: NSNotification.Name("NETWORK_RETRY"), object: nil)
                NotificationCenter.default.post(name: NSNotification.Name("NETWORK_TIMEOUT_RETRY"), object: nil)
            case .reachable(.cellular):
                switch self.cellularNetworkState() {
                case .CellularNetworkState2G:
                    print("2G")
                    self.currentNetworkState = .NetworkStatusCellular(.CellularNetworkState2G)
                case .CellularNetworkState3G:
                    print("3G")
                    self.currentNetworkState = .NetworkStatusCellular(.CellularNetworkState3G)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_RETRY"), object: nil)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_TIMEOUT_RETRY"), object: nil)
                case .CellularNetworkState4G:
                    print("4G")
                    self.currentNetworkState = .NetworkStatusCellular(.CellularNetworkState4G)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_RETRY"), object: nil)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_TIMEOUT_RETRY"), object: nil)
                case .CellularNetworkState5G:
                    print("5G")
                    self.currentNetworkState = .NetworkStatusCellular(.CellularNetworkState5G)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_RETRY"), object: nil)
                    NotificationCenter.default.post(name: NSNotification.Name("NETWORK_TIMEOUT_RETRY"), object: nil)
                default:
                    print("未知窝蜂网络")
                    self.currentNetworkState = .NetworkStatusUnkown
                }
            }
        })
    }
    
    private func cellularNetworkState() -> CellularNetworkState {
        let networkInfo = CTTelephonyNetworkInfo()
        var currentNetworkStr : String?
        var currentNet : CellularNetworkState = .CellularNetworkStateUnkown
        if #available(iOS 12.0,*) {
            let raduiDic = networkInfo.serviceCurrentRadioAccessTechnology
            if raduiDic != nil {
                if raduiDic!.keys.count != 0 {
                    currentNetworkStr = raduiDic!.first?.value
                }
            }
        }else{
            currentNetworkStr = networkInfo.currentRadioAccessTechnology
        }
        if  currentNetworkStr == CTRadioAccessTechnologyGPRS ||
            currentNetworkStr == CTRadioAccessTechnologyCDMA1x ||
            currentNetworkStr == CTRadioAccessTechnologyEdge {
            currentNet = .CellularNetworkState2G
        }else if currentNetworkStr == CTRadioAccessTechnologyWCDMA ||
                 currentNetworkStr == CTRadioAccessTechnologyHSDPA ||
                 currentNetworkStr == CTRadioAccessTechnologyHSUPA ||
                 currentNetworkStr == CTRadioAccessTechnologyCDMAEVDORev0 ||
                 currentNetworkStr == CTRadioAccessTechnologyCDMAEVDORevA ||
                 currentNetworkStr == CTRadioAccessTechnologyCDMAEVDORevB ||
                 currentNetworkStr == CTRadioAccessTechnologyeHRPD {
            currentNet = .CellularNetworkState3G
        }else if currentNetworkStr == CTRadioAccessTechnologyLTE {
            currentNet = .CellularNetworkState4G
        }else if #available(iOS 14.1, *) {
            if currentNetworkStr == CTRadioAccessTechnologyNRNSA ||
               currentNetworkStr == CTRadioAccessTechnologyNR {
                currentNet = .CellularNetworkState5G
            }
        }
        return currentNet
    }
}
